using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Collections.Generic;

[WebService(Namespace = "http://triptracker.org/",
    Name = "TripTracker Web Service",
   Description = "TripTracker Web Service interface to database")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    private SqlConnection connectObj;

    public Service()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    /// <summary>
    /// Conects to the database
    /// </summary>
    private void connect()
    {
        string server = "62.149.153.17";
        string database = "MSSql17385";
        string usr = "MSSql17385";
        string pw = "6c361361";

        string connectionString = "server=" + server + ";uid=" + usr + ";pwd=" + pw + ";database=" + database + ";";

        connectObj = new SqlConnection(connectionString);
    }

    /// <summary>
    /// Deletes all the coordinates of a given user
    /// </summary>
    /// <param name="user">The name of the user</param>
    [WebMethod]
    public void deleteUserCoordinates(String user)
    {
        connect();

        string queryString = "DELETE FROM Coordinates WHERE FK_user = '" + user + "'";
        SqlCommand commandObj = new SqlCommand(queryString, connectObj);

        connectObj.Open();
        commandObj.ExecuteNonQuery();
        connectObj.Close();

    }

    [WebMethod]
    public Boolean checkUser(string UserName, string Password)
    {
        connect();

        string queryString = "SELECT * FROM Users WHERE uname = '" + UserName + "' AND Pwd='" + Password + "'";
        SqlCommand commandObj = new SqlCommand(queryString, connectObj);
        SqlDataReader readerObj;

        connectObj.Open();
        readerObj = commandObj.ExecuteReader();

        if (readerObj.Read())
        {
            connectObj.Close();
            return true;
        }
        else
        {
            connectObj.Close();
            return false;
        }
    }

    /// <summary>
    /// Adds all the coordinates recived from a user to the database
    /// </summary>
    /// <param name="User">The name of the user to assign the coordinates</param>
    /// <param name="Coordinates">Contains coordinates in the format latitude|longitude|altitude|timestamp</param>
    /// <returns></returns>
    [WebMethod]
    public Boolean addCoordinates(string User, String[] Coordinates)   //Cannot use object that inherits from ICollection given that are not XML structured 
                                                                       //and/or are .NET specific (we loose the compatibbility of webservices)
    {
        connect();
        foreach(String Coordinate in Coordinates) {
            try
            {
                string[] CoordinateElements = Coordinate.Split('|');
                string queryString = "INSERT INTO Coordinates (FK_user, latitude, longitude, altitude, time) VALUES ( '" + User + "', '" + CoordinateElements[0] + "', '" + CoordinateElements[1] + "', '" + CoordinateElements[2] + "', '" + CoordinateElements[3] + "' )";
                SqlCommand commandObj = new SqlCommand(queryString, connectObj);
                connectObj.Open();
                commandObj.ExecuteReader();
                connectObj.Close();
            }
            catch {
                return false;
            }
        }
        
        return true; //return the correct results
    }
}